package com.bookcloud.java21.chapter11.集合元素遍历迭代器116;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/** 集合元素遍历迭代器116 */
public class IteratorExamples {
  public static void main(String[] args) {
    // 一、迭代列表类
    List<String> list = List.of("A", "B", "C", "D", "E");
    Iterator<String> iterator = list.iterator();
    while (iterator.hasNext()) {
      String element = iterator.next();
      System.out.println("迭代列表类->元素：" + element);
    }
    System.out.println("-----------------------");
    // 二、迭代集合类 set
    Set<String> set = Set.of("A", "B", "C");
    Iterator<String> iterator2 = set.iterator();
    while (iterator2.hasNext()) {
      String element = iterator2.next();
      System.out.println("迭代集合类->元素：" + element);
    }
    System.out.println("-----------------------");
    // 三、迭代Map
    Map<String, Integer> map = Map.of("A", 1, "B", 2, "C", 3);
    // Map.Entry<String, Integer> key,value的形式
    Iterator<Map.Entry<String, Integer>> iterator3 = map.entrySet().iterator();
    while (iterator3.hasNext()) {
      Map.Entry<String, Integer> entry = iterator3.next();
      System.out.println("迭代Map->键：" + entry.getKey() + "，值：" + entry.getValue());
    }
  }
}
